﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YuShiManageSystem.COM;
using YuShiManageSystem.DATA;
using YuShiManageSystem.UI;

namespace YuShiManageSystem.UI.Card
{
    /// <summary>
    /// 会员卡充值窗体
    /// </summary>
    public partial class TopUp : Base
    {
        private tb_card card;

        public TopUp()
            :base()
        {
            InitializeComponent();            
        }

        public TopUp(Session session)
            :base(session)
        {
            InitializeComponent();
            this.card = this.Session.DataModel.getCardByCard(this.code.Text);
            if (this.card == null)
            {
                YSMessage.Show("该卡不存在！");
                this.Close();
            }
            if (this.card.debt > 0)
            {
                YSMessage.Show("该卡欠款"+this.card.debt.ToString());
            }
            initStaffNo();
        }

        //初始化员工工号下拉框
        public void initStaffNo()
        {
            string[] arr1 = this.Session.DataModel.tb_person.OrderBy(t => t.personNo).Select(t => t.personNo).ToArray();
            string[] arr2 = this.Session.DataModel.tb_person.OrderBy(t => t.personNo).Select(t => t.pName).ToArray();
            string[] arr = new string[arr1.Length];
            for (int i = 0; i < arr1.Length; i++)
            {
                arr[i] = arr1[i] + "|" + arr2[i];
            }
            this.staffNo.DataSource = arr;
        }


        //staffNo选中值后为staffName赋值
        public void setStaffName()
        {
            
        }

        //code文本框textchange事件
        public void codeTextChange()
        {
            this.useAmount.Text = this.card.useAmount.ToString();
        }

        //保存事件
        public void save(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.code.Text))
                return;
            if (string.IsNullOrEmpty(this.subCode.Text))
            {
                YSMessage.Show("小单号不能为空！");
                return;
            }
            if (!Validator.Num(this.subCode.Text))
            {
                YSMessage.Show("小单号只能为数字！");
                return;
            }

            if (this.card == null)
                return;
            float dept = (float)(this.card.vipAmount - this.UpDownMoney.Value);
            if (dept > 0)
            {
                YSMessage.Confirm("该卡欠款" + dept.ToString());
            }
            this.card.useAmount = this.UpDownMoney.Value + this.card.useAmount;
            this.card.debt = dept > 0 ? (decimal)dept : 0;

            tb_order order = getOrder(this.card);

        }

        public tb_order getOrder(tb_card card)
        {
            tb_order order = new tb_order();
            order.storeId = card.storeId;
            order.cardId = card.cardId;
            order.customerType = Session.CUSTOMER_TYPE_VIP;

            return order;
        }
    }
}
